/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjtu.se1603.lh.dao.Impl;

import edu.fjtu.se1603.lh.dao.PetOwnerDao;
import edu.fjtu.se1603.lh.entity.PetOwner;
import edu.fjtu.se1603.lh.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class PetODaoImpl implements PetOwnerDao{

   

    @Override
    public int add(PetOwner owner) {
        int r=0;
        try {
            Connection con=DBUtil.getConnection();
            PreparedStatement pst=con.prepareStatement("insert into petowner(NAME,PASSWORD,MONEY)VALUES(?,?,?)");
            pst.setString(1, owner.getName());
            pst.setString(2, owner.getPwd());
            pst.setString(3, owner.getBalance());
            
            r= pst.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(PetODaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
                return r;
    }

    public PetOwner getById(int id) {
        PetOwner owner=null;
        try {
            Connection con=DBUtil.getConnection();
            ResultSet rs = con.createStatement().executeQuery("select*from petowner where OWNER_ID="+id);
            if(rs.next()){
           owner=new PetOwner();
           owner.setId(rs.getInt(1));
           owner.setName(rs.getString(2));
           owner.setPwd(rs.getString(3));
           owner.setBalance(rs.getString(4));                   
        System.out.println("第"+owner.getId()+"个宠物主人，"+"名字叫："+owner.getName());
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(PetODaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } 
        return owner;
    }
   

    @Override
    public List<PetOwner> getList() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<PetOwner> getList(int page, int rowcount) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public PetOwner getByname(String name) {
        PetOwner owner=null;
        try {
            Connection con=DBUtil.getConnection();
            ResultSet rs = con.createStatement().executeQuery("select*from petowner where NAME="+name);
            if(rs.next()){
           owner=new PetOwner();
           owner.setId(rs.getInt(1));
           owner.setName(rs.getString(2));
           owner.setPwd(rs.getString(3));
           owner.setBalance(rs.getString(4));                   
        System.out.println("名字："+owner.getName());
        System.out.println("元宝数："+owner.getBalance());
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(PetODaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } 
        return owner;
    }

    @Override
    public PetOwner getid(String name) {
       PetOwner owner=null;
        try {
            Connection con=DBUtil.getConnection();
            ResultSet rs = con.createStatement().executeQuery("select*from petowner where NAME="+name);
            if(rs.next()){
           owner=new PetOwner();
           owner.setId(rs.getInt(1));
                System.out.println("主人id："+owner.getId());
       
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(PetODaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } 
        return owner;
    }

   
    
}
